package com.gugee.ins.common.beanutil;

import com.gugee.ins.common.model.mysql.blogger.InsBlogger;
import com.gugee.ins.common.model.spider.blogger.InsBloggerUserFromId;
import io.micrometer.core.instrument.util.StringUtils;

/**
 * 转换生成insBlogger类
 * @author Huang xin
 */
public class InsBloggerConversion {

    private InsBloggerConversion(){

    }

    public static InsBlogger mapper(InsBloggerUserFromId dto) {
        if (dto == null) {
            return null;
        } else {
            InsBlogger insBlogger = new InsBlogger();
            insBlogger.setUid(getUid(dto));
            insBlogger.setUsername(dto.getUsername());
            insBlogger.setFullName(dto.getFullName());
            insBlogger.setPicUrl(dto.getPicUrl());
            insBlogger.setExternalUrl(dto.getExternalUrl());
            insBlogger.setBiography(getBiography(dto));
            insBlogger.setFollowingCount(dto.getFollowingCount());
            insBlogger.setFollowerCount(dto.getFollowerCount());
            insBlogger.setMediaCount(dto.getMediaCount());
            insBlogger.setIgtvCount(dto.getIgtvCount());
            insBlogger.setFollowingTagCount(dto.getFollowingTagCount());
            insBlogger.setUsertagsCount(dto.getUserTagsCount());
            insBlogger.setIsPrivate(dto.getIsPrivate());
            insBlogger.setIsVerified(dto.getIsVerified());
            insBlogger.setIsBusiness(dto.isBusiness());
            insBlogger.setAccountType(dto.getAccountType());
            insBlogger.setCategoryName(dto.getCategory());
            insBlogger.setContactPhoneNumber(dto.getContactPhoneNumber());
            insBlogger.setPublicEmail(getEmail(dto));
            insBlogger.setPublicPhoneCountryCode(dto.getPublicPhoneCountryCode());
            insBlogger.setPublicPhoneNumber(dto.getPublicPhoneNumber());
            insBlogger.setRegion(getCountry(dto));
            insBlogger.setCityId(dto.getCityId());
            insBlogger.setCityName(getCityName(dto));
            insBlogger.setIsAnonymousPic(dto.getIsAnonymousPic());

            return insBlogger;
        }
    }

    public static long getUid(InsBloggerUserFromId dto) {
        if (dto.getUid() == null) {
            return 0L;
        }
        return dto.getUid();
    }
    public static String getCountry(InsBloggerUserFromId dto) {
        if (StringUtils.isEmpty(dto.getCityName())) {
            return null;
        }
        String country = dto.getCityName().substring(dto.getCityName().lastIndexOf(",") + 1).trim();
        return country.length() > 63 ? country.substring(0,63) : country;
    }


    public static String getCityName(InsBloggerUserFromId dto) {
        if (StringUtils.isEmpty(dto.getCityName())) {
            return null;
        }
        return dto.getCityName().length() > 255 ? dto.getCityName().substring(0,255) : dto.getCityName();
    }

    public static String getBiography(InsBloggerUserFromId dto) {
        if (StringUtils.isEmpty(dto.getBiography())) {
            return null;
        }
        return dto.getBiography().length() > 1000 ? dto.getBiography().substring(0,1000) : dto.getBiography();
    }

    public static String getEmail(InsBloggerUserFromId dto) {
        if (StringUtils.isEmpty(dto.getPublicEmail())) {
            return null;
        }
        return dto.getPublicEmail().length() > 128 ? dto.getPublicEmail().substring(0,128) : dto.getPublicEmail();
    }
}
